<?php

namespace app\member\controller;

use app\admin\model\Login as ModelLogin;
use app\common\controller\Frontend;

class Login extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = 'default';

    public function index()
    {
        if (session("memberinfo")) {
            $this->error("请勿重复登陆");
        }
        $url = $this->request->request('url', '');
        $this->view->assign('url', $url);
        $this->view->assign('title', __('Login'));
        return $this->view->fetch();
    }

    // 登录提交
    public function postdata()
    {
        if (session("memberinfo")) {
            $this->error("请勿重复登陆");
        }
        $account = input('account','','trim');
        $password = input('password','','trim');
        if (empty($account)) {
            $this->error('用户名不能为空');
        }
        if (empty($password)) {
            $this->error('密码不能为空');
        }
        // 1.根据账号取出数据，取不出来数据说明没有这个用户
        // 2.根据取出的数据进行密码匹配，匹配不成功说明密码错误，重新登录 
        $memberinfo = ModelLogin::where(['username' => $account])->find();
        if (empty($memberinfo)) {
            $this->error("账号不存在");
        }
        if ($memberinfo['password'] != md5($password)) {
            $this->error("密码不正确");
        }
        // 登录成功之后把用户信息存储到session中
        session('memberinfo',$memberinfo);
        $this->success("登录成功", 'index/index/index');
    }

    // 退出登录
    public function logout()
    {
        session('memberinfo', null);
        $this->success("成功退出登录", 'index/index/index');
    }
}